﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using VERKProjectHopur16.Models;
using VERKProjectHopur16.Helpers;
using System.Web.Caching;

namespace VERKProjectHopur16.Controllers
{
    /// <summary>
    /// Controller for QA´s.
    /// </summary>
    [HandleError]
    public class QAController : ParentController
    {
        IDataRepository m_rep = null;

        public QAController()
        {
            m_rep = new DataRepository();
        }

        public QAController(IDataRepository rep)
        {
            m_rep = rep;
        }

        public ActionResult Index()
        {
            return View();
        }

        public ActionResult AllQA()
        {
            IEnumerable<QA> depAll;
            IEnumerable<QA> depMed;
            IEnumerable<QA> depPsy;
            IEnumerable<QA> depSos;
            String strSort = "InsertDate";

            if ( Session["QAOrder"] != null ) 
            {
                if ( Session["QAOrder"].Equals(1) )
                {
                    strSort = "Question";
                }
                else if ( Session["QAOrder"].Equals(2) )
                {
                    strSort = "InsertDate";
                }
                else if ( Session["QAOrder"].Equals(3) )
                {
                    strSort = "CreatorEmail";
                }
                else if ( Session["QAOrder"].Equals(4) )
                {
                    strSort = "Department.Name";
                }
            }

            // All questions and answers
            var tempResult = (from qa in m_rep.AllQA()
                              where qa.Answered == true
                              && qa.Active == true
                              select qa);
            
            // Questions ans answers filtered by department
            if (strSort == "InsertDate")
            {
                depAll = tempResult.AsQueryable().OrderByDescending(strSort);
                depMed = tempResult.Where(m => m.DepartmentID == 2).AsQueryable().OrderByDescending(strSort);
                depPsy = tempResult.Where(m => m.DepartmentID == 3).AsQueryable().OrderByDescending(strSort);
                depSos = tempResult.Where(m => m.DepartmentID == 4).AsQueryable().OrderByDescending(strSort);
            }
            else
            {
                depAll = tempResult.AsQueryable().OrderBy(strSort);
                depMed = tempResult.Where( m => m.DepartmentID == 2 ).AsQueryable().OrderBy(strSort);
                depPsy = tempResult.Where( m => m.DepartmentID == 3 ).AsQueryable().OrderBy(strSort);
                depSos = tempResult.Where( m => m.DepartmentID == 4 ).AsQueryable().OrderBy(strSort);
            }

            DepQAViewModel model = new DepQAViewModel();

            model.allDept = depAll;
            model.medDept = depMed;
            model.psiDept = depPsy;
            model.socDept = depSos;

            return View(model);
        }

        /// <summary>
        /// Helpers function to order QA´s
        /// </summary>
        public ActionResult OrderQA(int? id)
        {
            if ( id.HasValue )
            {
                Session["QAOrder"] = id;
            }
            return RedirectToAction("AllQA");
        }

        public ActionResult SingleQA(int? id)
        {
            if ( id.HasValue )
            {
                var QA_id = id.Value;
                var result = m_rep.GetQAById(QA_id);
                if ( result == null )
                {
                    return View("NotFound");
                }
                return View(result);
            }
            else
            {
                return View("NotFound");
            }
        }

        [HttpGet]
        public ActionResult CreateQA()
        {
            return View(new QAViewModel());
        }

        [HttpPost]
        public ActionResult CreateQA(QAViewModel s)
        {
            if ( ModelState.IsValid )
            {
                QA t = new QA();
                t.QAID = s.QAID;
                t.Question = s.Question;
                t.InsertDate = s.InsertDate;
                t.DepartmentID = Convert.ToInt32(s.DepartmentID);
                t.CreatorEmail = s.CreatorEmail;
                t.Answered = false;
                t.Active = true;
                m_rep.AddQA(t);
                return RedirectToAction("AllQA");
            }
            else
            {
                return View(s);
            }
        }
    }
}
